package com.example.demo.controller;
import  com.example.demo.service.GoodsService;
import  com.example.demo.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.Data;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/{id}")
    public ResponseEntity<Goods> getGoods(@PathVariable Long id){
        Goods result=goodsService.getGoods(id);
        if(result==null){
            return ResponseEntity.noContent().build();
        }else {
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("")
    public ResponseEntity<List<Goods>> findGoods(String name,Integer amount,Double price){
        List<Goods> result=goodsService.findGoods(name,amount,price);
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<Goods> addGoods(@RequestBody Goods good){
        Goods result=goodsService.addGoods((good));
        return ResponseEntity.ok(result);
    }
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateGoods(@PathVariable Long id,@RequestBody Goods good){
        goodsService.updateGoods(id,good);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable Long id){
        goodsService.deleteGoods(id);
        return ResponseEntity.ok().build();
    }
}